package group

import (
	"context"
	protos "goim-pro/api/protos/salty"
	. "goim-pro/examples/salty/helper"
	"goim-pro/pkg/logs"
	"goim-pro/pkg/utils"
)

var (
	logger = logs.GetLogger("INFO")
)

func CreateGroup(t protos.GroupServiceClient) {
	createGroupReq := &protos.CreateGroupReq{
		GroupName: "NEW_GROUP",
		MembersUserId: []string{
			GetUserA().UserId,
			GetUserB().UserId,
		},
	}
	anyData, _ := utils.MarshalMessageToAny(createGroupReq)
	grpcReq := &protos.GrpcReq{
		UserId:   GetUserA().UserId,
		DeviceId: "LOCAL_DEV",
		Version:  "",
		Language: 0,
		Os:       0,
		Token:    GetUserA().Token,
		Data:     anyData,
	}

	tr, err := t.CreateGroup(context.Background(), grpcReq)
	if err != nil {
		logger.Errorf("create group error: %s", err.Error())
	} else {
		PrintResp(tr, &protos.CreateGroupResp{})
	}
}

func GetGroups(t protos.GroupServiceClient) {
	getGroupReq := &protos.GetGroupsReq{}
	anyData, _ := utils.MarshalMessageToAny(getGroupReq)
	grpcReq := &protos.GrpcReq{
		UserId:   GetUserA().UserId,
		DeviceId: "LOCAL_DEV",
		Version:  "",
		Language: 0,
		Os:       0,
		Token:    GetUserA().Token,
		Data:     anyData,
	}

	tr, err := t.GetGroups(context.Background(), grpcReq)
	if err != nil {
		logger.Errorf("get group error: %s", err.Error())
	} else {
		PrintResp(tr, &protos.CreateGroupResp{})
	}
}
